package com.ruantu.clothing.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruantu.clothing.dao.UserRoleDao;
import com.ruantu.clothing.entity.UserRole;
import com.ruantu.clothing.service.UserRoleService;

@Controller
public class UserRoleController {

    @Autowired
    private UserRoleService urService;

    @Autowired
    private UserRoleDao userRoleDao;

    @RequestMapping("/addUserRole")
    @ResponseBody
    public int addUserRole(String ur) {
        List<UserRole> list = new ArrayList<UserRole>();
        ur = ur.substring(0, ur.length() - 1) + "]";
        JSONArray obj = JSONArray.parseArray(ur);// 解析String 成JSONARRy
        for (int i = 0; i < obj.size(); i++) {
            JSONObject jo = (JSONObject) obj.get(i);
            list.add(new UserRole(Integer.parseInt(jo.get("userid").toString()),
                    Integer.parseInt(jo.get("roleid").toString())));
        }
        urService.addUserRole(list);
        return 1;
    }

    @RequestMapping("/delUserRole")
    @ResponseBody
    public int delUserRole(int userid) {
        return urService.shanUserRole(userid);
    }

    @RequestMapping("/findRoleId")
    @ResponseBody
    public List<Integer> findRoleId(int userid) {
        return urService.findAllRoleId(userid);

    }

    @RequestMapping("/findCountR")
    @ResponseBody
    public int findCount(int roleid) {
        int count = userRoleDao.findCount(roleid);
        return count;
    }

}
